<template>
  <el-dialog
      title="活动报名审核"
      v-model="state.visible"
      width="500px"
  >
    <el-form :model="state.ruleForm" :rules="state.rules" ref="formRef" label-width="100px" class="good-form">
      <el-form-item label="审核状态" prop="reviewStatus">
        <el-radio-group type="text" v-model="state.ruleForm.reviewStatus">
          <el-radio value="已通过" border>通过</el-radio>
          <el-radio value="未通过" border>拒绝</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="审核意见" prop="reviewOpinion">
        <el-input type="textarea" v-model="state.ruleForm.reviewOpinion"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="state.visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue';
import { useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import {getActivityRegistrationById, reviewActivityRegistration} from "@/service/activityRegistrationApi.js";
import {addActivity, updateActivity} from "@/service/activityApi.js";

const props = defineProps({
  type: String, // 用于判断是添加还是编辑
  reload: Function // 添加或修改完后，刷新列表页
});

const formRef = ref(null);
const route = useRoute();
const state = reactive({
  visible: false,
  ruleForm: {
    reviewStatus: '',
    reviewOpinion: ''
  },
  rules: {
    reviewStatus: [
      { required: true, message: '审核状态不能为空', trigger: ['change'] }
    ],
    reviewOpinion: [
      { required: true, message: '审核意见不能为空', trigger: ['change'] }
    ]
  }
});

// 获取详情并回显数据
const getDetail = async (id) => {
  const result = await getActivityRegistrationById(id);
  state.ruleForm = {
    id: result.data.id,
    reviewStatus: result.data.reviewStatus,
    reviewOpinion: result.data.reviewOpinion
  };
};

// 开启弹窗
const open = (id) => {
  state.visible = true;
  if (id) {
    state.ruleForm.id = id;
    getDetail(id); // 回显数据
  } else {
    state.ruleForm = {
      id: '',
      reviewStatus: '',
      reviewOpinion: ''
    };
  }
};

// 关闭弹窗
const close = () => {
  state.visible = false;
};

// 提交表单
const submitForm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      if (props.type === 'add') {
        await addActivity(state.ruleForm).then(() => {
          ElMessage.success('添加成功');
          close();
          if (props.reload) props.reload();
        });
      } else {
        await reviewActivityRegistration(state.ruleForm).then(() => {
          ElMessage.success('修改成功');
          close();
          if (props.reload) props.reload();
        });
      }
    }
  });
};

defineExpose({ open, close });
</script>

<style scoped>
.el-upload__tip {
  float: left;
}
</style>
